<template>
  <StudentLayout>
    <el-button type="primary" :icon="Plus" @click="addCourseDig()">关联课程</el-button>
    <el-table :data="teacherToCourseData" style="width: 100%">
      <el-table-column prop="tea_id" label="教师编号" />
      <el-table-column prop="tea_name" label="教师姓名" />
      <el-table-column prop="cour_id" label="课程编号" />
      <el-table-column prop="cour_name" label="课程名称" />
      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" @click="addCourseDig(scope.row)">
            编辑
          </el-button>
          <el-popconfirm title="确认删除吗?"
            @confirm="delCourseFun(scope.row)"
          >
            <template #reference>
              <el-button
                size="small"
                type="danger"
              >
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </StudentLayout>
  <el-dialog v-model="dialogFormVisible" :title="isEdit?'编辑':'新增'"
    width="500">
    <el-form :model="form" @submit.native.prevent>
      <el-form-item label="教师">
        <el-select v-model="form.tea_id" placeholder="请选择教师">
          <el-option v-for="(item, index) in teacherData"
            :key="index"
            :label="item.tea_name"
            :value="item.tea_id" />
        </el-select>
      </el-form-item>
      <el-form-item label="课程">
        <el-select v-model="form.cour_id" placeholder="请选择课程">
          <el-option v-for="(item, index) in courseData"
            :key="index"
            :label="item.cour_name"
            :value="item.cour_id" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">关闭</el-button>
        <el-button type="primary" @click="addCourseSub()">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import { ElMessage } from 'element-plus'
import StudentLayout from '@/layouts/StudentLayout.vue'

import { getTeacherToCourseList, delTeacherToCourse, getTeacherList, getCourseList, addTeacherToCourse, editTeacherToCourse } from '@/composables/school'

const teacherData = ref([])
const teacherReq = async () => {
   await getTeacherList().then(res => {
    teacherData.value = res.data
  })
}

const courseData = ref([])
const courseReq = async () => {
   await getCourseList().then(res => {
    courseData.value = res.data
  })
}

const teacherToCourseData = ref()
const teacherToCourse = async () => {
  await getTeacherToCourseList().then(res => {
    teacherToCourseData.value = res.data
  })
}

const isEdit = ref(false)
const dialogFormVisible = ref(false)
const form = reactive({
  tea_id: '',
  cour_id: ''
})
const addCourseDig = (item) => {
  isEdit.value = false
  dialogFormVisible.value = true
  delete form._id
  form.tea_id = ''
  form.cour_id = ''
  if(item) {
    form._id = item._id
    form.tea_id = item.tea_id
    form.cour_id = item.cour_id
    isEdit.value = true
  }
}

const delCourseFun = async (row) => {
  const {_id} = row
  await delTeacherToCourse({_id}).then(res => {
    if (res.code === 200) {
      ElMessage({
        message: '操作成功',
        type: 'success',
      })
      teacherToCourse()
    }
  })
}

const addCourseSub = async () => {
  if(isEdit.value) {
    await editTeacherToCourse(form).then(res => {
      if (res.code === 200) {
        ElMessage({
          message: '操作成功',
          type: 'success',
        })
        teacherToCourse()
        dialogFormVisible.value = false
      }
    })
  } else {
    await addTeacherToCourse(form).then(res => {
      if (res.code === 200) {
        ElMessage({
          message: '操作成功',
          type: 'success',
        })
        teacherToCourse()
        dialogFormVisible.value = false
      }
    })
  }
}
onMounted(() => {
  teacherReq()
  courseReq()
  teacherToCourse()
})
</script>
<style scoped>

</style>